﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AAA.Meta.Quote;
using AAA.Meta.Quote.Data;

namespace AAA.Quote
{
    public abstract class AbstractRealtimeQuote : IRealtimeQuote
    {
        private int _iQuotePeriod;
        private bool _isStarted;

        private event TickDataHandler _tickDataHandler;
        private event TrustDataHandler _trustDataHandler;

        private List<string> _lstWatchList;

        public AbstractRealtimeQuote()
            : this(10)
        {
        }


        public AbstractRealtimeQuote(int iQuotePeriod)
        {
            _iQuotePeriod = iQuotePeriod;
            _lstWatchList = new List<string>();
        }
        #region IRealtimeQuote Members

        public void AddWatchSymbol(string strSymbolId)
        {
            if (_lstWatchList.IndexOf(strSymbolId) > -1)
                return;
            _lstWatchList.Add(strSymbolId);
            if (IsStarted)
            {
                StopQuote();
                StartQuote();
            }
        }

        public void RemoveWatchSymbol(string strSymbolId)
        {
            if (_lstWatchList.IndexOf(strSymbolId) < 0)
                return;
            _lstWatchList.Remove(strSymbolId);
            if (IsStarted)
            {
                StopQuote();
                StartQuote();
            }
        }

        public List<string> GetWatchList()
        {
            return _lstWatchList;
        }

        public TickDataHandler TickDataReceiveEvent
        {
            get { return _tickDataHandler; }
            set { _tickDataHandler = value; }
        }

        public TrustDataHandler TrustDataReceiveEvent
        {
            get { return _trustDataHandler; }
            set { _trustDataHandler = value; }
        }

        public int QuotePeriod
        {
            get
            {
                return _iQuotePeriod;
            }
            set
            {
                _iQuotePeriod = value <= 0 ? 1 : value;
            }
        }

        public bool IsStarted
        {
            get
            {
                return _isStarted;
            }
            set
            {
                _isStarted = value;
            }
        }

        public void OnDataReceive(AAA.Meta.Quote.Data.TickData tickData)
        {
            if (_tickDataHandler != null)
                _tickDataHandler(tickData);            
        }

        public void OnTrustDataReceive(AAA.Meta.Quote.Data.TrustData trustData)
        {
            if (_trustDataHandler != null)
                _trustDataHandler(trustData);            
        }

//        public abstract void OnDataReceive(TickData tickData);
//        public abstract void OnTrustDataReceive(TrustData tickData);
        public abstract string Request(string strItem);
        public abstract bool StartQuote();        
        public abstract bool StopQuote();
        #endregion
    }
}
